package com.edittable.servlet;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.edittable.dao.EmpInfoDao;
import com.edittable.dao.IEmpInfoDao;
import com.edittable.util.PageModel;

public class PageServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private IEmpInfoDao empInfoDao=null;
	public PageServlet() {
		super();
	}
	public void destroy() {
		super.destroy(); // Just puts "destroy" string in log
		// Put your code here
	}
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doPost(request,response);
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		response.setContentType("text/html");
		String pageStr=request.getParameter("page");
		if(pageStr==""||pageStr==null){
			pageStr="1";
		}
		int page=Integer.parseInt(pageStr);
		PageModel pageModel=new PageModel();
		pageModel.setCurrPage(page);
		pageModel.setPageSize(5);
		pageModel.setTotalRecords(empInfoDao.getCount());
		pageModel.setList(empInfoDao.selectForPage((page-1)*5, page*5));
		request.setAttribute("pageModel", pageModel);
		request.getRequestDispatcher("pagetable.jsp").forward(request, response);
	}

	public void init() throws ServletException {
		empInfoDao=new EmpInfoDao();	
	}

}
